{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "[[1.0000000e+00 6.9945578e-09]\n",
      " [1.0000000e+00 7.1988768e-18]\n",
      " [1.0000000e+00 1.1208689e-17]\n",
      " [1.0000000e+00 3.4045591e-10]\n",
      " [1.0000000e+00 4.3896901e-13]\n",
      " [1.0000000e+00 7.7841864e-21]\n",
      " [9.8104972e-01 1.8950211e-02]\n",
      " [1.0000000e+00 1.9985809e-09]\n",
      " [9.8016775e-01 1.9832209e-02]\n",
      " [1.0000000e+00 9.9073411e-26]\n",
      " [3.1085649e-01 6.8914354e-01]\n",
      " [1.0000000e+00 2.3639506e-19]\n",
      " [9.8899007e-01 1.1009884e-02]\n",
      " [1.0000000e+00 5.6962665e-15]\n",
      " [1.0000000e+00 6.0987926e-10]\n",
      " [1.0000000e+00 5.1555362e-24]]\n",
      "WRONG: MVIMG_20190725_121952_786.jpg ---- 0\n",
      "WRONG: MVIMG_20190725_121915_000.jpg ---- 0\n",
      "WRONG: MVIMG_20190725_121051_938.jpg ---- 0\n",
      "WRONG: MVIMG_20190725_121926_411.jpg ---- 0\n",
      "WRONG: final_image_Wed Sep 18 15:55:59 GMT+07:00 2019.jpg ---- 0\n",
      "WRONG: MVIMG_20190725_121944_289.jpg ---- 0\n",
      "WRONG: MVIMG_20190725_121934_740.jpg ---- 0\n",
      "WRONG: MVIMG_20190725_121056_215.jpg ---- 0\n",
      "WRONG: IMG_20190725_130928.jpg ---- 0\n",
      "WRONG: MVIMG_20190725_121904_173.jpg ---- 0\n",
      "WRONG: final_image_Wed Sep 18 15:52:50 GMT+07:00 2019.jpg ---- 0\n",
      "WRONG: final_image_Wed Sep 18 15:55:02 GMT+07:00 2019.jpg ---- 0\n",
      "WRONG: final_image_Wed Sep 18 15:53:43 GMT+07:00 2019.jpg ---- 0\n",
      "WRONG: final_image_Wed Sep 18 15:56:29 GMT+07:00 2019.jpg ---- 0\n",
      "WRONG: MVIMG_20190725_121919_136.jpg ---- 0\n",
      "[[1.0000000e+00 4.9280380e-21]\n",
      " [1.0000000e+00 1.7280195e-19]\n",
      " [1.0000000e+00 4.2521545e-15]\n",
      " [1.0000000e+00 5.3701904e-28]]\n",
      "WRONG: final_image_Wed Sep 18 15:55:34 GMT+07:00 2019.jpg ---- 0\n",
      "WRONG: final_image_Wed Sep 18 15:53:19 GMT+07:00 2019.jpg ---- 0\n",
      "WRONG: MVIMG_20190725_121950_638.jpg ---- 0\n",
      "WRONG: IMG_20190725_130924.jpg ---- 0\n",
      "COUNT:  19\n",
      "RESULT: 0.95\n"
     ]
    }
   ],
   "source": [
    "import keras as K\n",
    "from tensorflow.keras.models import load_model\n",
    "import pywt\n",
    "import numpy as np\n",
    "import cv2\n",
    "import os\n",
    "import tensorflow as tf\n",
    "from keras.backend.tensorflow_backend import set_session\n",
    "import shutil\n",
    "# os.environ[\"CUDA_VISIBLE_DEVICES\"]=\"\"\n",
    "# print(os.listdir(\"/media/liem/hai/haihh/dataset/classify/classify/train\"))\n",
    "config = tf.ConfigProto()\n",
    "config.gpu_options.allow_growth = True  # dynamically grow the memory used on the GPU\n",
    "config.log_device_placement = True  # to log device placement (on which device the operation ran)  #chay server thi comment\n",
    "sess = tf.Session(config=config)\n",
    "set_session(sess)  # set this TensorFlow session as the default\n",
    "\n",
    "def scale(x, mode=0, axis=None):\n",
    "    xmax, xmin = x.max(), x.min()\n",
    "    x = (x - xmin)/(xmax - xmin)\n",
    "    if mode != 0:\n",
    "        x = (x - 0.5) * 2\n",
    "    return x\n",
    "\n",
    "def DWT(img):\n",
    "    coeffs2 = pywt.dwt2(img, 'bior1.3')\n",
    "    LL, (LH, HL, HH) = coeffs2     #500x402x4\n",
    "    norm_LL = scale(LL, 0, 2)\n",
    "    norm_LH = scale(LH, -1, 2)\n",
    "    norm_HL = scale(HL, -1, 2)\n",
    "    return norm_LL, norm_LH, norm_HL\n",
    "\n",
    "model = load_model(\"weights-25-0.9087.hdf5\")\n",
    "PATH = \"test_recapture\"\n",
    "\n",
    "GT = 1\n",
    "batch_size = 16\n",
    "list_file = os.listdir(PATH)\n",
    "# print(list_file)\n",
    "step = len(list_file) // batch_size\n",
    "print(step)\n",
    "count = 0\n",
    "for iteration in range(step + 1):\n",
    "    batch_ll = []\n",
    "    batch_lh = []\n",
    "    batch_hl = []\n",
    "    files = []\n",
    "    for item in range(batch_size):\n",
    "        idx = item + iteration * batch_size\n",
    "        if idx >= len(list_file):\n",
    "            continue\n",
    "        img_path = os.path.join(PATH, list_file[idx])\n",
    "        img = cv2.imread(img_path)\n",
    "        ll0, lh0, hl0 = DWT(img)\n",
    "        batch_ll.append(ll0)\n",
    "        batch_lh.append(lh0)\n",
    "        batch_hl.append(hl0)\n",
    "        \n",
    "#         img = cv2.resize(img, (224, 224))\n",
    "#         batch.append(img)\n",
    "        files.append(list_file[idx])\n",
    "    if(len(batch_ll) == 0):\n",
    "        continue\n",
    "    batch_ll = np.asarray(batch_ll)\n",
    "    batch_lh = np.asarray(batch_lh)\n",
    "    batch_hl = np.asarray(batch_hl)\n",
    "    \n",
    "    res = model.predict([batch_ll, batch_lh, batch_hl])\n",
    "    print(res)\n",
    "    class_res = np.argmax(res, axis=1)\n",
    "    for iii in range(len(class_res)):\n",
    "        if(class_res[iii] != GT):\n",
    "            print(\"WRONG: {} ---- {}\".format(files[iii], class_res[iii]))\n",
    "            shutil.copy(os.path.join(PATH, files[iii]), os.path.join(\"wrong\", files[iii]))\n",
    "            count += 1\n",
    "print(\"COUNT: \", count)\n",
    "print(\"RESULT: {}\".format(count/len(list_file)))\n",
    "cv2.destroyAllWindows()\n",
    "# print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python hai env",
   "language": "python",
   "name": "hai"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
